package com.starblink.imgsearch.findsimilar.ui.view

import android.graphics.*
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.extension.dp2px

/**
 *  实现单reccyelerview滚动折叠效果
 *  Create by yuanl at 2023/4/13
 **/
internal class SimilarHeaderCollapseDecorator(
    private val targetView: View
) : RecyclerView.ItemDecoration() {

    private val bgColor = targetView.context.color(com.starblink.basic.style.R.color.sk_gray50)

    private val marginLeft = 12.dp2px()

    private val marginBottom = 8.dp2px()

    private val bgRadius = 32.dp2px().toFloat()

    private val bgPaint = Paint().apply {
        color = bgColor
        isAntiAlias = true
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        if (position < 1) {
            if (targetView.measuredHeight <= 0) {
                targetView.measure(
                    View.MeasureSpec.makeMeasureSpec(
                        parent.measuredWidth - marginLeft.shl(1),
                        View.MeasureSpec.EXACTLY
                    ),
                    View.MeasureSpec.makeMeasureSpec(
                        parent.measuredHeight,
                        View.MeasureSpec.AT_MOST
                    )
                )
            }
            outRect.set(0, targetView.measuredHeight + marginBottom, 0, 0)
        } else {
            super.getItemOffsets(outRect, view, parent, state)
        }
        //Log.d(TAG, "-->> getItemOffsets, position: ${position} / outRect: $outRect")

    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)

        targetView.layout(parent.left, 0, parent.right, targetView.measuredHeight)

        // Log.d(TAG, "-->> onDraw: height: ${targetView.measuredHeight} ")

        val bottom = parent.bottom + 400
        for (i in 0 until parent.childCount) {
            val view = parent.getChildAt(i)
            val position = parent.getChildAdapterPosition(view)

            if (position == 0) {
                //var saved = c.save()
                //c.clipRect(parent.left, parent.top, parent.right, view.top)
                val height = targetView.measuredHeight
                var top = (view.top - height) * 0
                c.translate(marginLeft.toFloat(), 0f)
                targetView.draw(c)
                c.translate(-marginLeft.toFloat(), 0f)

                //c.restoreToCount(saved)

                val saved = c.save()
                top = if (view.top < 0) parent.top else view.top
                c.clipRect(
                    parent.left,
                    top,
                    parent.right,
                    parent.bottom + 300
                )
                c.drawRoundRect(
                    parent.left.toFloat(), top.toFloat(), parent.right.toFloat(),
                    bottom.toFloat(), bgRadius, bgRadius, bgPaint
                )
                c.restoreToCount(saved)

            } else {
                val saved = c.save()

                c.clipRect(parent.left, view.top, parent.right, bottom)

                c.drawRoundRect(
                    parent.left.toFloat(),
                    parent.top.toFloat(),
                    parent.right.toFloat(),
                    bottom.toFloat(),
                    bgRadius,
                    bgRadius,
                    bgPaint
                )
                c.restoreToCount(saved)

            }
            // ！！！不用clipPath 由于滚动时会导致绘制卡顿，gpu占用高
        }


    }


    companion object {

        private const val TAG = "HeaderParallaxDecorator"

        @JvmStatic
        fun attach(recyclerView: RecyclerView, target: View) {
            recyclerView.addItemDecoration(SimilarHeaderCollapseDecorator(target))
        }

    }


}